﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;

namespace YouHP.BLL.MedicalGraph
{
    
    public class GraphVariableRule : ISerializable
    {
        public string mName;
        public string mShortName;
        public string mKey;
        public string mUnit;
        private Color mColor;


        public Brush mBrush;
        public Pen mPen;
        public Font mFont;
        public Font mFont2;
        public Pen mAxisPen;

        public List<double> mUpper = new List<double>();
        public List<double> mLower = new List<double>();
        public List<Brush> mRangBrush = new List<Brush>();
        public List<string> mRangeName = new List<string>();

        public void AddRange(double lower, double upper, Color color, string name)
        {
            mLower.Add(lower);
            mUpper.Add(upper);
            mRangBrush.Add(new SolidBrush(color));
            mRangeName.Add(name);
        }
        public List<double> mGridValue = new List<double>();

        internal double GetRelPosition(double value)
        {
            List<double> vallist = mGridValue;
            double retval = 0.0;

            double lastval = vallist[0];
            if (value < lastval || Math.Abs(lastval - value) < 0.001)
            {
                retval = value / lastval;
            }
            else
            {
                retval = 1.0;
                int cnt = vallist.Count;
                lastval = vallist[0];
                for (int i = 1; i < cnt; i++)
                {
                    double val = vallist[i];
                    if (value < val || Math.Abs(val - value) < 0.001)
                    {
                        retval += (value - lastval) / (val - lastval);
                        break;
                    }
                    else
                    {
                        retval += 1.0f;
                        lastval = val;
                    }
                }
            }

            retval /= vallist.Count;
            return retval;
        }

        public void SetColor(Color color)
        {
            mColor = color;
            mFont = new Font("宋体", 12.0f);
            mFont2 = new Font("宋体", 12.0f);
            mBrush = new SolidBrush(color);
            mAxisPen = new Pen(color, 2.0f);
            mAxisPen.CustomEndCap = new AdjustableArrowCap(4, 6);
            mPen = new Pen(color, 2.0f);
        }

        double mNormalPosition;
        double mNewPercent;
        double mNormalValue;

        public double GetNormalPosition()
        {
            mNormalValue = this.mUpper[0];
            mNormalPosition = GetRelPosition(mNormalValue);
            return mNormalPosition;
        }
        public void SetNormalPosition(double positon)
        {
            mNewPercent = positon;

        }

        public double GetRelPosition2(double value)
        {
            double percent = GetRelPosition(value);
            if (value < mNormalValue) return (percent / mNormalPosition * mNewPercent);
            else return (percent - mNormalPosition) / (1 - mNormalPosition) * (1 - mNewPercent) + mNewPercent;
        }

        #region ISerializable 成员

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
        }

        #endregion
    }
}
